package cn.itsource.org.controller;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.util.ExcelUtil;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.service.IShopService;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageBean<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.pageList(query);
    }


    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        shopService.settlement(shop);
        return AjaxResult.me();
    }

    /**
     * 审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        shopService.auditPass(shopAuditLog);
        return AjaxResult.me();
    }


    /**
     * 导出店铺数据
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        //查询所有数据
        List<Shop> shops = shopService.queryAll();

        ExcelUtil.exportExcel(shops,"哈哈哈","sheet1",Shop.class,"店铺信息.xlsx",response);
    }


    @PostMapping("/import_excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/statistics")
    public AjaxResult statistics(){
        HashMap<String, List<Integer>> hm = shopService.storeStatistics();
        return AjaxResult.me().setResultObj(hm);
    }
}
